'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 邮件
 * @author sundbcn@gmail.com (sun debing)
 */
var moment = require('moment');
var _ = require('lodash');
var modelUtil = require('../util/modelUtil');
var mongoConnector = require('../libs/mongodb/mongoConnector');
/* jshint unused:false */
var mailSchema = require('../schemas/mailSchema');
var Mail = mongoConnector().model('Mail');

/**
 * 根据role_id获取用户
 * @return {[type]} [description]
 */
module.exports.getByRoleId = function (role_id, cb) {
    modelUtil.getByRoleId(Mail, role_id, function (err, mail_model) {
        if (!!err) {
            console.error(err);
            return cb(err);
        }

        _arrangeMail(mail_model);
        cb(null, mail_model);
    });
};

/**
 * 删除过期邮件
 * @param  {[type]}   mail_model [description]
 */
var _arrangeMail = function (mail_model) {
    var mail_list = mail_model.getMailList();
    _.each(mail_list, function (mail_item) {
        if (moment().diff(moment(mail_item.create_time), 'd') >= 15) {
            mail_model.deleteMail(mail_item.mail_id);
        }
    });
};
